package com.bridgeintelligent.tag.customer.controller;

import com.bridgeintelligent.tag.customer.dto.CustomersDto;
import com.bridgeintelligent.tag.customer.entity.CustomersPage;
import com.bridgeintelligent.tag.customer.model.IssueModel;
import com.bridgeintelligent.tag.customer.model.IssueQueryModel;
import com.bridgeintelligent.tag.customer.pojo.Customers;
import com.bridgeintelligent.tag.customer.service.IIssueService;
import com.bridgeintelligent.tag.customer.vo.IssueVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @description
 * @author: grp 2021-02-23 13:52
 **/
@Api(tags = "下发管理")
@RestController
@AllArgsConstructor
@Validated
@Slf4j
@RequestMapping("api/issue")
public class IssueController {

    IIssueService issueService;

    @ApiOperation(value = "新增下发", notes = "下发管理")
    @PostMapping("add")
    public CustomersDto<Object> add(@RequestBody @Valid IssueModel model) {
        if(!NumberUtils.isDigits(model.getCcis())){
            return CustomersDto.error("ccis工单名称必须为数字！");
        }
        int count = issueService.countById(model.getIssueKey());
        if(count>0){
            return CustomersDto.error("下发id不能重复！");
        }
        count = issueService.countByName(model.getIssueName());
        if(count>0){
            return CustomersDto.error("下发名称重复，请修改下发名称后重新提交！");
        }
        issueService.add(model);
        return CustomersDto.success();
    }

    @ApiOperation(value = "修改下发", notes = "下发管理")
    @PostMapping("put")
    public CustomersDto<Object> put(@RequestBody @Valid IssueModel model) {
        if(StringUtils.isBlank(model.getChannelId())){
            return CustomersDto.error("下发id不能为空！");
        }
        int count = issueService.countByNameAndNotId(model.getIssueKey(),model.getIssueName());
        if(count>0){
            return CustomersDto.error("下发名称重复，请修改下发名称后重新提交！");
        }
        if(!NumberUtils.isDigits(model.getCcis())){
            return CustomersDto.error("ccis工单名称必须为数字！");
        }
        issueService.put(model);
        return CustomersDto.success();
    }

    @ApiOperation(value = "下发列表", notes = "下发管理")
    @GetMapping("list")
    public CustomersDto<CustomersPage<IssueVO>> list(@ModelAttribute @Valid IssueQueryModel model) {
        model.setPageNumber(model.getPageNumber() == null ? 1 : model.getPageNumber());
        model.setPageSize(model.getPageSize() == null ? 10 : model.getPageSize());
        CustomersPage<IssueVO> page = issueService.list(model);
        return CustomersDto.success(page);
    }

    @ApiOperation(value = "下发删除", notes = "下发管理")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "下发ID", required = true)})
    @DeleteMapping("del")
    public CustomersDto<Object> del(@NotBlank String id) {
        issueService.del(id);
        return CustomersDto.success();
    }

    @ApiOperation(value = "详情", notes = "下发管理")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "下发id", required = true)})
    @GetMapping("detail")
    public CustomersDto<IssueVO> detail(@NotBlank String id) {
        return CustomersDto.success(issueService.getDetail(id));
    }

    @ApiOperation(value = "客群", notes = "下发管理")
    @GetMapping("customers")
    public CustomersDto<List<Customers>> customers() {
        List<Customers> customers = issueService.findCustomers();
        return CustomersDto.success(customers);
    }

    @ApiOperation(value = "修改状态", notes = "下发管理")
    @PostMapping("changState")
    public CustomersDto<Object> changState(@RequestBody IssueModel model) {
        if(StringUtils.isBlank(model.getId())){
            return CustomersDto.error("id不能为空！");
        }
        if(StringUtils.isBlank(model.getState())){
            return CustomersDto.error("状态不能为空！");
        }
        issueService.changState(model);
        return CustomersDto.success();
    }
}
